<style lang="scss">
#family{
    height:100%;
    background:#fff;
    .cmain-content{
        height:100%;
        position: relative;
    }
}
</style>

<template>
    <div id="family" v-loading="loading" element-loading-text="数据加载中，请稍等......">
        <input id="hiddenFlag" type="hidden" v-model="hiddenFlag"></input>
        <input id="editType" type="hidden" v-model="editType"></input>
        <input id="patientId" type="hidden" v-model="patientId"></input>
        <input id="familyTreeId" type="hidden" v-model="familyTreeId"></input>
        <input id="probandData" type="hidden" v-model="probandData"></input>
        <input id="familyJSONData" type="hidden" v-model="familyJSONData"></input>
        <input id="pedigreeData" type="hidden" v-model="pedigreeData"></input>
        <div class="cmain-content" >
            <iframe name="childWin" v-if="iframeShow && !isWarn" src="/static/familyTree/test.html" width="100%" height="100%" frameborder="0"></iframe>
            <warn v-if="isWarn && !iframeShow" @callback="getFamilyData"></warn>
        </div>
    </div>
</template>
<script>
import {
    familyTree
} from 'api/index.js';
import { setCookie, getCookie, delCookie } from '@/common/js/cookie.js';
import warn from "@/components/warn.vue";
export default {
    name:"draw",
    data(){
        return {
            isWarn:false,
            loading: false,
            probandData:"",
            familyJSONData:"",
            pedigreeData:"",
            iframeShow:false,
            editType:"",
            familyTreeId:"",
            patientId:"",
            hiddenFlag:false
        }
    },
    props:["hiddenDom","edtype","familyid"],
    methods:{
        getFamilyData(){
            var obj={
                familyTreeId:this.familyTreeId,
            }
            this.loading = true;
            familyTree.findByFamilyTreeId(obj).then((res) => {
                if(res.returnCode == 0 || res.returnCode == 200){
                    if(res.data && res.data.length>0){
                        var probandData = this.getProbandData(res.data);
                        this.probandData = JSON.stringify(probandData);
                        var pedigreeData = res.data;
                        this.pedigreeData = JSON.stringify(pedigreeData);
                        var familyJSONData = res.data;
                        this.familyJSONData = JSON.stringify(familyJSONData);
                        this.iframeShow = true;
                    }else{
                        if(this.editType == "add"){
                            this.probandData = sessionStorage.getItem("probandData");
                            this.pedigreeData = sessionStorage.getItem("pedigreeData");
                            this.familyJSONData = sessionStorage.getItem("familyJSONData");
                            this.iframeShow = true;
                        }else{
                            this.probandData = "";
                            this.pedigreeData = "";
                            this.familyJSONData = "";
                            this.$Message.error("暂无数据");
                        }
                    }
                }else{
                    this.probandData = "";
                    this.pedigreeData = "";
                    this.familyJSONData = "";
                    this.$Message.error(res.msg);
                } 
                this.loading = false;
                this.isWarn = false;
            }).catch(error => {   
                this.isWarn = true;
                this.loading = false;
            });
        },
        //获取先证者信息
        getProbandData(data){
            var ret = {};
            for(var i=0;i<data.length;i++){
                if(data[i].precursor == 1){
                    ret = data[i];
                    break;
                }
            }
            return ret;
        }
    },
    created() {
       this.editType = M.url().drawState ? M.url().drawState : this.edtype;
       this.familyTreeId = M.url().familyTreeId ? M.url().familyTreeId : this.familyid;
       this.patientId = M.url().patientId ? M.url().patientId : "";
       if(this.editType == "edit"){
           sessionStorage.removeItem("familyJSONData"); 
           sessionStorage.removeItem("probandData"); 
           sessionStorage.removeItem("pedigreeData"); 
       }
       this.getFamilyData();
       this.hiddenFlag = this.hiddenDom ? this.hiddenDom : false;
    },
    mounted() {
        
    },
    components:{
        warn
    }
}
</script>
  